// 格式化日期
export const formatDate = (DateObj) => {
  const year = DateObj.getFullYear().toString()
  const month = (DateObj.getMonth() + 1).toString().padStart(2, '0')
  const day = DateObj.getDate().toString().padStart(2, '0')
  const hour = DateObj.getHours().toString().padStart(2, '0')
  const minute = DateObj.getMinutes().toString().padStart(2, '0')
  const second = DateObj.getSeconds().toString().padStart(2, '0')
  return `${year}-${month}-${day} ${hour}:${minute}:${second}`
}

// 格式化日期（年月日）
export const formYmd = (DateObj) => {
  const year = DateObj.getFullYear().toString()
  const month = (DateObj.getMonth() + 1).toString().padStart(2, '0')
  const day = DateObj.getDate().toString().padStart(2, '0')
  return `${year}-${month}-${day}`
}

// 获取昨日日期
export const getayer = () => {
  const today = new Date()
  const year = today.getFullYear().toString()
  const month = (today.getMonth() + 1).toString().padStart(2, '0')
  const day = (today.getDate() - 1).toString().padStart(2, '0')
  const date = `${year}-${month}-${day}`
  return [date, date]
}

// 获取近7日的日期
export const getSevenDays = () => {
  const today = new Date(new Date().toLocaleDateString()).getTime()
  const startTime = today - 7 * 3600 * 24 * 1000
  const endTime = today - 1
  const startDate = formYmd(new Date(startTime))
  const endDate = formYmd(new Date(endTime))
  return [startDate, endDate]
}

// 获取近30日的日期
export const getThirtyDays = () => {
  const today = new Date(new Date().toLocaleDateString()).getTime()
  const startTime = today - 30 * 3600 * 24 * 1000
  const endTime = today - 1
  const startDate = formYmd(new Date(startTime))
  const endDate = formYmd(new Date(endTime))
  return [startDate, endDate]
}

// 获取本周的日期
export const getThisWeekDate = () => {
  const today = new Date(new Date().toLocaleDateString()).getTime()
  const nowDayOfWeek = new Date().getDay()
  const startTime = today - ((nowDayOfWeek === 0) ? 6 : nowDayOfWeek - 1) * 24 * 60 * 60 * 1000
  const endTime = nowDayOfWeek === 0 ? today : today + (7 - nowDayOfWeek) * 24 * 60 * 60 * 1000
  const startDate = formYmd(new Date(startTime))
  const endDate = formYmd(new Date(endTime))
  return [startDate, endDate]
}

// 获取本月的日期
export const getThisMonthDate = () => {
  const year = new Date().getFullYear()
  const month = new Date().getMonth()
  const monthStartData = new Date(year, month, 1).getTime()
  const monthEndData = new Date(year, month + 1, 0).getTime() + 24 * 60 * 60 * 1000 - 1
  const monthStart = formYmd(new Date(monthStartData))
  const monthEnd = formYmd(new Date(monthEndData))
  return [monthStart, monthEnd]
}
